#!/bin/bash

set -ux

# 网络
hostname=server163
net=ens160
ifcfg=/etc/sysconfig/network-scripts/ifcfg-$net
ip=192.168.20.163
gateway=192.168.20.2
dns1=192.168.20.2

# 第2个ip
ip_1=192.168.10.163

echo "设置hostname $hostname"
hostnamectl --static set-hostname $hostname

echo '修改ssh配置'
sed -i -e '/^#UseDNS/c UseDNS no' \
  -e '/^GSSAPIAuthentication/c GSSAPIAuthentication no' \
  /etc/ssh/sshd_config &&
  systemctl restart sshd

echo "修改ip $ifcfg"
# 删除和修改
sed -i \
  -e "/^IPADDR=/d" \
  -e "/^PREFIX=/d" \
  -e "/^GATEWAY=/d" \
  -e "/^DNS1=/d" \
  -e '/^ONBOOT=*/c ONBOOT="yes"' \
  -e '/^BOOTPROTO=*/c BOOTPROTO="static"' \
  $ifcfg

# 添加
cat >> $ifcfg << EOF
IPADDR="$ip"
PREFIX=24
GATEWAY="$gateway"
DNS1="$dns1"
EOF

echo '配置IP HOST映射关系'
cat >> /etc/hosts << EOF
192.168.20.160 server160
192.168.20.161 server161
192.168.20.162 server162
192.168.20.163 server163
192.168.20.164 server164
192.168.20.165 server165
EOF

echo "查看 $ifcfg"
cat $ifcfg

echo '配置多ip'
cat > $ifcfg:1 << EOF
DEVICE=$net:1
Type=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=$ip_1
PREFIX=24
EOF

echo "查看 $ifcfg:1"
cat $ifcfg:1

echo '重启网卡'
nmcli c reload &&
  nmcli c up $net
